package com.lagou;

import com.alibaba.fastjson.JSON;
import com.lagou.service.UserServiceImpl;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.data.Stat;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.HashMap;

@SpringBootApplication
public class ServerBootstrap {
    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(ServerBootstrap.class, args);

        UserServiceImpl.startServer("127.0.0.1",8990);
        UserServiceImpl.createHostToZookeeper();

        //修改数据库连接信息节点测试
        saveDBInfoToZookeeper();
    }

    public static void saveDBInfoToZookeeper(){
        HashMap<String, String> env = new HashMap<>();
        env.put("driver","com.mysql.jdbc.Driver");
        env.put("url","jdbc:mysql://localhost:3306/test");
        env.put("username","root");
        env.put("password","root");
        String json= JSON.toJSONString(env);

        //创建zookeeper节点保存数据库连接信息
        ZkClient zkClient = new ZkClient("114.215.81.54:2181");
        boolean exists = zkClient.exists("/DBInfo");
        if(!exists){
            zkClient.createPersistent("/DBInfo",json);
        }else{
            zkClient.writeData("/DBInfo",json);
        }
    }
}
